Node.js File System தொகுதிக்கு அறிமுகம்
Node.js File System தொகுதி (fs) உங்கள் கணினியில் உள்ள கோப்பு முறைமையுடன் பணிபுரிய ஒரு விரிவான முறைகளின் தொகுப்பை வழங்குகிறது.
இது ஒத்திசைவு மற்றும் ஒத்திசைவற்ற வழிகளில் கோப்பு I/O செயல்பாடுகளை செய்ய உங்களை அனுமதிக்கிறது.
குறிப்பு:
File System தொகுதி Node.js இன் முக்கிய தொகுதியாகும், எனவே எந்த நிறுவலும் தேவையில்லை.
கோப்பு செயல்பாடுகள்
கோப்புகளைப் படித்தல் மற்றும் எழுதுதல்
அடைவு செயல்பாடுகள்
அடைவுகளை உருவாக்குதல் மற்றும் நீக்குதல்
மேம்பட்ட அம்சங்கள்
கோப்பு ஸ்ட்ரீம்கள், கோப்பு கண்காணிப்பு
File System தொகுதியை இறக்குமதி செய்தல்
நீங்கள் CommonJS require() அல்லது ES தொகுதிகள் import இலக்கணத்தைப் பயன்படுத்தி File System தொகுதியை இறக்குமதி செய்யலாம்:
CommonJS (Node.js இல் இயல்புநிலை)
const fs = require('fs');
ES Modules (Node.js 14+ with "type": "module" in package.json)
import fs from 'fs';
// Or for specific methods:
// import { readFile, writeFile } from 'fs/promises';
Promise-அடிப்படையிலான API
Node.js fs/promises பெயர்வெளியில் File System API இன் promise-அடிப்படையிலான பதிப்புகளை வழங்குகிறது, இது நவீன பயன்பாடுகளுக்கு பரிந்துரைக்கப்படுகிறது:
// Using promises (Node.js 10.0.0+)
const fs = require('fs').promises;
// Or with destructuring
const { readFile, writeFile } = require('fs').promises;
// Or with ES modules
// import { readFile, writeFile } from 'fs/promises';
பொதுவான பயன்பாட்டு நிகழ்வுகள்
கோப்பு செயல்பாடுகள்
- கோப்புகளைப் படித்தல் மற்றும் எழுதுதல்
- கோப்புகளை உருவாக்குதல் மற்றும் நீக்குதல்
- கோப்புகளில் சேர்த்தல்
- கோப்புகளை மறுபெயரிடுதல் மற்றும் நகர்த்துதல்
- கோப்பு அனுமதிகளை மாற்றுதல்
அடைவு செயல்பாடுகள்
- அடைவுகளை உருவாக்குதல் மற்றும் அகற்றுதல்
- அடைவு உள்ளடக்கங்களை பட்டியலிடுதல்
- கோப்பு மாற்றங்களைக் கண்காணித்தல்
- கோப்பு/அடைவு புள்ளிவிவரங்களைப் பெறுதல்
- கோப்பு இருப்பைச் சரிபார்த்தல்
மேம்பட்ட அம்சங்கள்
- கோப்பு ஸ்ட்ரீம்கள்
- கோப்பு விவரிப்பான்கள்
- சின்ன இணைப்புகள்
- கோப்பு கண்காணிப்பு
- கோப்பு அனுமதிகளுடன் பணிபுரிதல்
செயல்திறன் உதவிக்குறிப்பு:
பெரிய கோப்புகளுக்கு, உயர் நினைவக பயன்பாட்டைத் தவிர்க்க ஸ்ட்ரீம்களை (fs.createReadStream மற்றும் fs.createWriteStream) பயன்படுத்தவும்.
கோப்புகளைப் படித்தல்
Node.js கோப்புகளைப் படிக்க பல முறைகளை வழங்குகிறது, callback-அடிப்படையிலான மற்றும் promise-அடிப்படையிலான அணுகுமுறைகள் உட்பட.
மிகவும் பொதுவான முறை fs.readFile() ஆகும்.
குறிப்பு:
கோப்பு செயல்பாடுகளுடன் பணிபுரியும் போது எப்போதும் பிழைகளைக் கையாளவும், இது உங்கள் பயன்பாட்டை செயலிழக்காமல் தடுக்கும்.
Callbacks உடன் கோப்புகளைப் படித்தல்
பாரம்பரிய callback முறையைப் பயன்படுத்தி கோப்பைப் படிக்க இங்கே எப்படி:
myfile.txt
This is the content of myfile.txt
எடுத்துக்காட்டு: Callbacks உடன் கோப்பைப் படித்தல்
const fs = require('fs');
// Read file asynchronously with callback
fs.readFile('myfile.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
console.log('File content:', data);
});
// For binary data (like images), omit the encoding
fs.readFile('image.png', (err, data) => {
if (err) throw err;
// data is a Buffer containing the file content
console.log('Image size:', data.length, 'bytes');
});
Promises உடன் கோப்புகளைப் படித்தல் (நவீன அணுகுமுறை)
எடுத்துக்காட்டு: async/await உடன் கோப்பைப் படித்தல்
// Using fs.promises (Node.js 10.0.0+)
const fs = require('fs').promises;
async function readFileExample() {
try {
const data = await fs.readFile('myfile.txt', 'utf8');
console.log('File content:', data);
} catch (err) {
console.error('Error reading file:', err);
}
}
readFileExample();
// Or with util.promisify (Node.js 8.0.0+)
const { promisify } = require('util');
const readFileAsync = promisify(require('fs').readFile);
async function readWithPromisify() {
try {
const data = await readFileAsync('myfile.txt', 'utf8');
console.log(data);
} catch (err) {
console.error(err);
}
}
readWithPromisify();
ஒத்திசைவாக கோப்புகளைப் படித்தல்
எடுத்துக்காட்டு: ஒத்திசைவாக கோப்பைப் படித்தல்
const fs = require('fs');
try {
// Read file synchronously
const data = fs.readFileSync('myfile.txt', 'utf8');
console.log('File content:', data);
} catch (err) {
console.error('Error reading file:', err);
}
சிறந்த நடைமுறை:
உரை கோப்புகளைப் படிக்கும்போது எப்போதும் எழுத்து குறியீட்டை ('utf8' போன்றவை) குறிப்பிடவும், இது Buffer க்கு பதிலாக ஒரு சரத்தைப் பெறும்.
கோப்புகளை உருவாக்குதல் மற்றும் எழுதுதல்
Node.js கோப்புகளை உருவாக்கவும் எழுதவும் பல முறைகளை வழங்குகிறது.
மிகவும் பொதுவான அணுகுமுறைகள் இங்கே:
1. fs.writeFile() பயன்படுத்துதல்
எடுத்துக்காட்டு: கோப்பில் எழுதுதல்
const fs = require('fs').promises;
async function writeFileExample() {
try {
// Write text to a file
await fs.writeFile('myfile.txt', 'Hello, World!', 'utf8');
// Write JSON data
const data = { name: 'John', age: 30, city: 'New York' };
await fs.writeFile('data.json', JSON.stringify(data, null, 2), 'utf8');
console.log('Files created successfully');
} catch (err) {
console.error('Error writing files:', err);
}
}
writeFileExample();
2. fs.appendFile() பயன்படுத்துதல்
எடுத்துக்காட்டு: கோப்பில் சேர்த்தல்
const fs = require('fs').promises;
async function appendToFile() {
try {
// Append a timestamped log entry
const logEntry = `${new Date().toISOString()}: Application started\n`;
await fs.appendFile('app.log', logEntry, 'utf8');
console.log('Log entry added');
} catch (err) {
console.error('Error appending to file:', err);
}
}
appendToFile();
3. கோப்பு கைப்பிடிகளைப் பயன்படுத்துதல்
எடுத்துக்காட்டு: கோப்பு கைப்பிடிகளைப் பயன்படுத்துதல்
const fs = require('fs').promises;
async function writeWithFileHandle() {
let fileHandle;
try {
// Open a file for writing (creates if doesn't exist)
fileHandle = await fs.open('output.txt', 'w');
// Write content to the file
await fileHandle.write('First line\n');
await fileHandle.write('Second line\n');
await fileHandle.write('Third line\n');
console.log('Content written successfully');
} catch (err) {
console.error('Error writing to file:', err);
} finally {
// Always close the file handle
if (fileHandle) {
await fileHandle.close();
}
}
}
writeWithFileHandle();
4. பெரிய கோப்புகளுக்கு ஸ்ட்ரீம்களைப் பயன்படுத்துதல்
எடுத்துக்காட்டு: ஸ்ட்ரீம்களுடன் பெரிய கோப்புகளை எழுதுதல்
const fs = require('fs');
const { pipeline } = require('stream/promises');
const { Readable } = require('stream');
async function writeLargeFile() {
// Create a readable stream (could be from HTTP request, etc.)
const data = Array(1000).fill().map((_, i) => `Line ${i + 1}: ${'x'.repeat(100)}\n`);
const readable = Readable.from(data);
// Create a writable stream to a file
const writable = fs.createWriteStream('large-file.txt');
try {
// Pipe the data from readable to writable
await pipeline(readable, writable);
console.log('Large file written successfully');
} catch (err) {
console.error('Error writing file:', err);
}
}
writeLargeFile();
கோப்பு கொடிகள்
| கொடி | விளக்கம் |
|---|---|
| 'w' | எழுதுவதற்காக திறக்கவும் (கோப்பு உருவாக்கப்பட்டது அல்லது சுருக்கப்பட்டது) |
| 'wx' | 'w' போன்றது ஆனால் பாதை இருந்தால் தோல்வியடைகிறது |
| 'w+' | படிப்பதற்கும் எழுதுவதற்கும் திறக்கவும் (கோப்பு உருவாக்கப்பட்டது அல்லது சுருக்கப்பட்டது) |
| 'a' | சேர்ப்பதற்காக திறக்கவும் (கோப்பு இல்லையென்றால் உருவாக்கப்படும்) |
| 'ax' | 'a' போன்றது ஆனால் பாதை இருந்தால் தோல்வியடைகிறது |
| 'r+' | படிப்பதற்கும் எழுதுவதற்கும் திறக்கவும் (கோப்பு இருக்க வேண்டும்) |
கோப்புகள் மற்றும் அடைவுகளை நீக்குதல்
Node.js கோப்புகள் மற்றும் அடைவுகளை நீக்க பல முறைகளை வழங்குகிறது.
வெவ்வேறு நீக்கல் சூழ்நிலைகளைக் கையாள இங்கே எப்படி:
1. ஒற்றை கோப்பை நீக்குதல்
எடுத்துக்காட்டு: கோப்பை நீக்குதல்
const fs = require('fs').promises;
async function deleteFile() {
const filePath = 'file-to-delete.txt';
try {
// Check if file exists before deleting
await fs.access(filePath);
// Delete the file
await fs.unlink(filePath);
console.log('File deleted successfully');
} catch (err) {
if (err.code === 'ENOENT') {
console.log('File does not exist');
} else {
console.error('Error deleting file:', err);
}
}
}
deleteFile();
2. பல கோப்புகளை நீக்குதல்
எடுத்துக்காட்டு: பல கோப்புகளை நீக்குதல்
const fs = require('fs').promises;
const path = require('path');
async function deleteFiles() {
const filesToDelete = [
'temp1.txt',
'temp2.txt',
'temp3.txt'
];
try {
// Delete all files in parallel
await Promise.all(
filesToDelete.map(file =>
fs.unlink(file).catch(err => {
if (err.code !== 'ENOENT') {
console.error(`Error deleting ${file}:`, err);
}
})
)
);
console.log('Files deleted successfully');
} catch (err) {
console.error('Error during file deletion:', err);
}
}
deleteFiles();
3. அடைவுகளை நீக்குதல்
எடுத்துக்காட்டு: அடைவுகளை நீக்குதல்
const fs = require('fs').promises;
const path = require('path');
async function deleteDirectory(dirPath) {
try {
// Check if the directory exists
const stats = await fs.stat(dirPath);
if (!stats.isDirectory()) {
console.log('Path is not a directory');
return;
}
// For Node.js 14.14.0+ (recommended)
await fs.rm(dirPath, { recursive: true, force: true });
// For older Node.js versions (deprecated but still works)
// await fs.rmdir(dirPath, { recursive: true });
console.log('Directory deleted successfully');
} catch (err) {
if (err.code === 'ENOENT') {
console.log('Directory does not exist');
} else {
console.error('Error deleting directory:', err);
}
}
}
// Usage
deleteDirectory('directory-to-delete');
4. அடைவை காலியாக்குதல் (நீக்காமல்)
எடுத்துக்காட்டு: அடைவை காலியாக்குதல்
const fs = require('fs').promises;
const path = require('path');
async function emptyDirectory(dirPath) {
try {
// Read the directory
const files = await fs.readdir(dirPath, { withFileTypes: true });
// Delete all files and directories in parallel
await Promise.all(
files.map(file => {
const fullPath = path.join(dirPath, file.name);
return file.isDirectory()
? fs.rm(fullPath, { recursive: true, force: true })
: fs.unlink(fullPath);
})
);
console.log('Directory emptied successfully');
} catch (err) {
console.error('Error emptying directory:', err);
}
}
// Usage
emptyDirectory('directory-to-empty');
பாதுகாப்பு குறிப்பு:
கோப்பு நீக்குதலுடன் மிகவும் கவனமாக இருங்கள், குறிப்பாக சுழல்நிலை விருப்பங்கள் அல்லது வைல்ட்கார்டுகளைப் பயன்படுத்தும் போது. அடைவு டிராவர்சல் தாக்குதல்களைத் தடுக்க கோப்பு பாதைகளை எப்போதும் சரிபார்க்கவும் மற்றும் சுத்திகரிக்கவும்.
கோப்புகளை மறுபெயரிடுதல் மற்றும் நகர்த்துதல்
fs.rename() முறையை கோப்பு பாதைகளை மாற்றுவதை உள்ளடக்கிய கோப்பு முறைமை செயல்பாடுகளுக்கான இரு வகையிலும் பயன்படுத்தலாம்.
1. அடிப்படை கோப்பு மறுபெயரிடுதல்
எடுத்துக்காட்டு: கோப்பை மறுபெயரிடுதல்
const fs = require('fs').promises;
async function renameFile() {
const oldPath = 'old-name.txt';
const newPath = 'new-name.txt';
try {
// Check if source file exists
await fs.access(oldPath);
// Check if destination file already exists
try {
await fs.access(newPath);
console.log('Destination file already exists');
return;
} catch (err) {
// Destination doesn't exist, safe to proceed
}
// Perform the rename
await fs.rename(oldPath, newPath);
console.log('File renamed successfully');
} catch (err) {
if (err.code === 'ENOENT') {
console.log('Source file does not exist');
} else {
console.error('Error renaming file:', err);
}
}
}
// Usage
renameFile();
2. அடைவுகளுக்கு இடையே கோப்புகளை நகர்த்துதல்
எடுத்துக்காட்டு: கோப்பை வெவ்வேறு அடைவுக்கு நகர்த்துதல்
const fs = require('fs').promises;
const path = require('path');
async function moveFile() {
const sourceFile = 'source/file.txt';
const targetDir = 'destination';
const targetFile = path.join(targetDir, 'file.txt');
try {
// Ensure source file exists
await fs.access(sourceFile);
// Create target directory if it doesn't exist
await fs.mkdir(targetDir, { recursive: true });
// Move the file
await fs.rename(sourceFile, targetFile);
console.log('File moved successfully');
} catch (err) {
if (err.code === 'ENOENT') {
console.log('Source file does not exist');
} else if (err.code === 'EXDEV') {
console.log('Cross-device move detected, using copy+delete fallback');
await moveAcrossDevices(sourceFile, targetFile);
} else {
console.error('Error moving file:', err);
}
}
}
// Helper function for cross-device moves
async function moveAcrossDevices(source, target) {
try {
// Copy the file
await fs.copyFile(source, target);
// Delete the original
await fs.unlink(source);
console.log('File moved across devices successfully');
} catch (err) {
// Clean up if something went wrong
try { await fs.unlink(target); } catch (e) {}
throw err;
}
}
// Usage
moveFile();
3. தொகுதி மறுபெயரிடுதல் கோப்புகள்
எடுத்துக்காட்டு: தொகுதி மறுபெயரிடுதல் கோப்புகள்
const fs = require('fs').promises;
const path = require('path');
async function batchRename() {
const directory = 'images';
const pattern = /^image(\d+)\.jpg$/;
try {
// Read directory contents
const files = await fs.readdir(directory);
// Process each file
for (const file of files) {
const match = file.match(pattern);
if (match) {
const [_, number] = match;
const newName = `photo-${number.padStart(3, '0')}.jpg`;
const oldPath = path.join(directory, file);
const newPath = path.join(directory, newName);
// Skip if the new name is the same as the old name
if (oldPath !== newPath) {
await fs.rename(oldPath, newPath);
console.log(`Renamed: ${file} - ${newName}`);
}
}
}
console.log('Batch rename completed');
} catch (err) {
console.error('Error during batch rename:', err);
}
}
batchRename();
4. அணு மறுபெயரிடுதல் செயல்பாடுகள்
எடுத்துக்காட்டு: அணு கோப்பு புதுப்பிப்பு
const fs = require('fs').promises;
const path = require('path');
const os = require('os');
async function updateFileAtomic(filePath, newContent) {
const tempPath = path.join(
os.tmpdir(),
`temp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
);
try {
// 1. Write to temp file
await fs.writeFile(tempPath, newContent, 'utf8');
// 2. Verify the temp file was written correctly
const stats = await fs.stat(tempPath);
if (stats.size === 0) {
throw new Error('Temporary file is empty');
}
// 3. Rename (atomic on most systems)
await fs.rename(tempPath, filePath);
console.log('File updated atomically');
} catch (err) {
// Clean up temp file if it exists
try { await fs.unlink(tempPath); } catch (e) {}
console.error('Atomic update failed:', err);
throw err;
}
}
// Usage
updateFileAtomic('important-config.json', JSON.stringify({ key: 'value' }, null, 2));
குறுக்கு-மேடை குறிப்பு:
fs.rename() செயல்பாடு யூனிக்ஸ் போன்ற அமைப்புகளில் அணுவாக உள்ளது ஆனால் விண்டோஸில் இல்லாமல் இருக்கலாம்.
குறுக்கு-மேடை அணு செயல்பாடுகளுக்கு, மேலே உள்ள எடுத்துக்காட்டில் காட்டப்பட்டுள்ளபடி தற்காலிக கோப்பு அணுகுமுறையைப் பயன்படுத்தவும்.
பயிற்சி
வாக்கியத்தை முடிக்க சரியான வார்த்தையை இழுத்து விடவும்.
Node.js provides both ______ and asynchronous methods for file operations.